<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends MY_Controller {
    /**
     * @var ConsumerOrderModel
     */
    public $mOrder = null;
    /**
     * @var ConsumerPromotionModel
     */
    public $mPromotion = null;
    /**
     * @var MerchantBalanceModel
     */
    public $mBalance = null;
    /**
     * @var ConsumerBuyModel
     */
    public $mBuy = null;

	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerOrderModel', 'mOrder', 'consumer');
        $this->loadModel('ConsumerPromotionModel', 'mPromotion', 'consumer');
        $this->loadModel('MerchantBalanceModel', 'mBalance', 'merchant');
        $this->loadModel('ConsumerBuyModel', 'mBuy', 'consumer');
	}


    /**
     * 首页
     */
    public function index(){
        // 最近1个月
        $startTime = strtotime("-1 month");
        $data = array(
            'startTime' => dateFormat($startTime),
            'endTime' => dateFormat(time()),
        );
        $this->view('statistics/admin/index', $data, 'datatable,date_range_picker');
    }

    public function getList(){
        $timeGap = trim(post('timegap'));
        if (!$timeGap){
            // 最近1个月
            $startTime = strtotime("-1 month");
            $endTime = 0;
        }else{
            $timeGapArr = explode(' - ', $timeGap);
            $startTime = strtotime($timeGapArr[0]);
            $endTime = strtotime($timeGapArr[1]);
        }

        $data = $this->mOrder->getWebMoneyList($startTime, $endTime);
        $bLog = $this->mPromotion->getTotalBonusLog($startTime, $endTime);
        $merBLog = $this->mBalance->getTotalBalanceLog($startTime, $endTime);
        $buyLog = $this->mBuy->getTotalBuyLog($startTime, $endTime);

        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $v['time'];
            $data['aaData'][$k][] = $v['reality_amount'];
            $data['aaData'][$k][] = $v['total_amount'];
            $data['aaData'][$k][] = $v['order_num'];
            $data['aaData'][$k][] = $v['closure'];
            $data['aaData'][$k][] = isset($bLog[$v['time']]) ? $bLog[$v['time']]['money'] : 0;
            $data['aaData'][$k][] = isset($merBLog[$v['time']]) ? $merBLog[$v['time']]['amount'] : 0;
            $data['aaData'][$k][] = isset($merBLog[$v['time']]) ? $merBLog[$v['time']]['total'] : 0;
            $data['aaData'][$k][] = isset($buyLog[$v['time']]) ? $buyLog[$v['time']]['money'] : 0;
            $data['aaData'][$k][] = isset($buyLog[$v['time']]) ? $buyLog[$v['time']]['total'] : 0;
        }
        unset($data['data']);
        echo json_encode($data);

    }

    /**
     * 导出
     * @author defcu
     */
    public function export(){
        $timeGap = trim(get('timegap'));
        $timeGapArr = explode(' - ', $timeGap);
        $startTime = strtotime($timeGapArr[0]);
        $endTime = strtotime($timeGapArr[1]);
        $filename = '平台资金信息'.$startTime.'-'.$endTime.'.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo mb_convert_encoding('时间,平台沉淀资金,总销售额,总订单量,交易关闭订单,红包总成本,商家提现金额,商家提现次数,消费者提现金额,消费者提现次数',
                'gb2312','utf-8')."\n";
        $pageNumber = 500;
        $page = 1;
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        while(true){
            $list = $this->mOrder->getWebMoneyList($startTime, $endTime, 0, $page, $pageNumber);
            $bLog = $this->mPromotion->getTotalBonusLog($startTime, $endTime, $page, $pageNumber);
            $merBLog = $this->mBalance->getTotalBalanceLog($startTime, $endTime, $page, $pageNumber);
            $buyLog = $this->mBuy->getTotalBuyLog($startTime, $endTime, $page, $pageNumber);
            if(empty($list)){
                break;
            }
            $page ++;
            foreach($list as $v){
                $bLMoney = isset($bLog[$v['time']]) ? $bLog[$v['time']]['money'] : 0;
                $BLAmount = isset($merBLog[$v['time']]) ? $merBLog[$v['time']]['amount'] : 0;
                $BLTotal = isset($merBLog[$v['time']]) ? $merBLog[$v['time']]['total'] : 0;
                $buyLMoney = isset($buyLog[$v['time']]) ? $buyLog[$v['time']]['money'] : 0;
                $buyLTotal = isset($buyLog[$v['time']]) ? $buyLog[$v['time']]['total'] : 0;
                echo mb_convert_encoding($v['time'].','.$v['reality_amount'].','.$v['total_amount'].','.
                        $v['order_num'].','.$v['closure'].','.$bLMoney.','.$BLAmount.','.$BLTotal.','.$buyLMoney.','.
                        $buyLTotal, 'gb2312','utf-8')."\n";
            }
        }
    }

}
